#![no_std]
#![no_main]

mod c_src;

pub mod static_mem;

pub use c_src::osPriority_t;
pub use c_src::osStatus_t;
pub use c_src::osThreadState_t;
pub use c_src::osWaitForever;
use c_src::SystemInit;

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "alloc")]
pub mod dynamic;

#[inline(always)]
pub fn os_kernel_initialize() {
    unsafe { c_src::osKernelInitialize(); }
}

#[inline(always)]
pub fn os_kernel_start() {
    unsafe { c_src::osKernelStart(); }
}

#[inline(always)]
pub fn os_kernel_get_tick_count() -> u32 {
    unsafe { c_src::osKernelGetTickCount() }
}

pub struct Instant {
    tick: u32,
}

impl Instant {

    pub fn new() -> Self {
        Self { tick: os_kernel_get_tick_count() }
    }

    pub fn reset(&mut self) {
        self.tick = os_kernel_get_tick_count();
    }

    pub fn elapsed(&self) -> u32 {
        os_kernel_get_tick_count().wrapping_sub(self.tick)
    }
}

#[inline(always)]
pub fn os_delay(ticks: u32) {
    unsafe { c_src::osDelay(ticks); }
}

#[inline(always)]
pub fn os_delay_until(ticks: u32) {
    unsafe { c_src::osDelayUntil(ticks); }
}

#[inline(always)]
pub fn system_init() {
    unsafe { SystemInit(); }
}
